import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';
import type { SopSysConfigModel } from '#/api/devOperation/sop/model/sopSysConfigModel';

import { ref } from 'vue';

import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';

import { SopSysConfigApi } from '#/api/devOperation/sop/sopSysConfig';
import { ActionEnum } from '#/enums/commonEnum';
import { PermCode } from '#/enums/perm';
import {
  createdTimeColumn,
  deleteButton,
  indexColumn,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();
const permCode = PermCode.devOperation.sop.sopSysConfig;
type ContextRef = object;

export function createCrudOptions(
  props: CreateCrudOptionsProps<
    SopSysConfigModel.SopSysConfigResultVO,
    ContextRef
  >,
): CreateCrudOptionsRet<SopSysConfigModel.SopSysConfigResultVO> {
  const selectedRowKeys = ref<string[]>([]);
  return {
    crudOptions: {
      request: {
        pageRequest: SopSysConfigApi.pageRequest,
        addRequest: SopSysConfigApi.addRequest,
        editRequest: SopSysConfigApi.editRequest,
        delRequest: SopSysConfigApi.delRequest,
        infoRequest: SopSysConfigApi.infoRequest,
      },
      actionbar: {
        buttons: {
          add: { show: hasPermission(permCode.add) },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: SopSysConfigApi.remove,
            role: permCode.delete,
          }),
        },
      },
      form: {
        wrapper: {
          is: 'a-modal',
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '200px',
        buttons: {
          edit: { show: hasPermission(permCode.edit) },
          view: { show: true },
          remove: { show: hasPermission(permCode.delete) },
          copy: { show: hasPermission(permCode.add) },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        configKey: {
          title: $t('devOperation.sop.sopSysConfig.configKey'),
          type: 'text',
          search: { show: true },
        },
        configValue: {
          title: $t('devOperation.sop.sopSysConfig.configValue'),
          type: 'text',
          search: { show: true },
        },
        remark: {
          title: $t('devOperation.sop.sopSysConfig.remark'),
          type: 'textarea',
          search: { show: true },
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = (
  _crudExpose: CrudExpose,
  _mode: ActionEnum,
): FormRulesExt => {
  return {};
};
